<template>
  <div>

    <div class="mt-[32px]">
      <el-form label-width="85px" :rules="rules" :model="form" ref="FormRef">
        <el-form-item :label="item.name" :prop="item.code"
                      v-for="(item,index) in config"
                      :key="index"
        >

          <el-input v-model="form[item.code]"
                    v-if="!item.type"
          />
          <el-input v-model="form[item.code]"
                    v-if="item.type == 'digit'"
                    type="number"
          />

          <el-select v-model="form[item.code]"
                     v-if="item.type == 'select'"
          >
            <el-option v-for="(it,idx) in item.list" :key="idx"
                       :label="it[item.label]" :value="it[item.value]"
            />
          </el-select>

          <UploadImg v-model="form[item.code]" v-if="item.type == 'upload'"/>

        </el-form-item>
      </el-form>
    </div>

  </div>
</template>
<script setup lang="ts">
import UploadImg from "@/components/UploadFile/src/UploadImg.vue"
import Icon from "@/components/Icon/src/Icon.vue";
import {listToMap, formError} from "@/utils";
import {GlobeStateApi} from "@/api/logistics/globestate";

const props = defineProps({
  info: {
    type: Object,
    required: false,
    default: () => {
    }
  }
})

const FormRef = ref(null)
const config = ref([
  {
    name: "地区",
    code: "stateName",
    required: true
  },
  {
    name: "语言",
    code: "language",
    required: true
  },
  {
    name: "币种简写",
    code: "currency",
    required: true
  },
  {
    name: "币种符号",
    code: "currencySymbol",
    required: true
  },
  {
    name: "国旗区旗",
    code: "img",
    type: "upload"
  },
  {
    name: "汇率",
    code: "exchangeRate",
    required: true
  },
  {
    name: "时区",
    code: "date",
  },
  {
    name: "邮编",
    code: "stateMailbox",
  },
  {
    name: "电话格式",
    code: "phoneFormat",
  },
  {
    name: "电话号码长度",
    code: "phoneNumber",
    type: "digit"
  },
])

const rules = computed(() => {
  let map = listToMap(config.value, 'code');

  for (let i in map) {
    if (map[i].required) {
      if (map[i].type == 'select' || map[i].type == 'upload') {
        map[i] = [{required: true, message: `请选择${map[i].name}`, trigger: 'change'}]
      } else {
        map[i] = [{required: true, message: `请输入${map[i].name}`, trigger: 'blur'}]
      }
    }

  }
  return map
})


const form = ref({})

const confirm = async (callback) => {

  try {
    let res = await FormRef.value.validate();


    let option = {
      ...form.value,
    }

    if (form.value.id) {
      await GlobeStateApi.updateGlobeState(option);
    } else {
      await GlobeStateApi.createGlobeState(option);
    }
    callback && callback()
  } catch (e) {
    console.log("22222222", e)
    formError()
  }

}


onMounted(() => {
})

defineExpose({
  confirm
})

watch(() => props.info, () => {
  form.value = props.info
}, {
  deep: true,
  immediate: true
})
</script>
<style lang="scss" scoped>

</style>
